简书链接:原创smarttable扩展model自定义列名
文章字数:106,阅读全文大约需要1分钟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

/**
* 获取Map中所有字段
* 暂时只支持Map中List数据解析 不支持数组[]
*/
public static void getMapColumn(List<Column> columns, String fieldName, String parentKey, List<Object> mapList, IFormat<String> keyFormat) {
if (mapList != null && mapList.size() > 0) {
Object o = mapList.get(0);
if (o != null) {
if (o instanceof Map) {
Map<String, Object> map = (Map<String, Object>) o;
//暂时只能解析json每个层级一个Array
boolean isOneArray = true;
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object val = entry.getValue();
if (ArrayColumn.isList(val)) {
if (isOneArray) {
List<Object> list = ((List) val);
getMapColumn(columns, fieldName + key + ".", key, list, keyFormat);
isOneArray = false;
}
} else {
String columnName = keyFormat == null ? key : keyFormat.format(key);
MapColumn<Object> column = new MapColumn<>(columnName, fieldName + key);
columns.add(column);
}
}
} else if (o instanceof CustomColumn) {
CustomColumn customColumn = (CustomColumn) o;
String[] keys = customColumn.getColumns();
//暂时只能解析json每个层级一个Array
boolean isOneArray = true;
if (keys != null) {
for (String key : keys) {
Object val = customColumn.getValueByColumnName(key);
if (ArrayColumn.isList(val)) {
if (isOneArray) {
List<Object> list = ((List) val);
getMapColumn(columns, fieldName + key + ".", key, list, keyFormat);
isOneArray = false;
}
} else {
String columnName = keyFormat == null ? key : keyFormat.format(key);
MapColumn<Object> column = new MapColumn<>(columnName, fieldName + key);
columns.add(column);
}
}
}
} else {
String columnName = keyFormat == null ? parentKey : keyFormat.format(parentKey);
MapColumn<Object> column = new MapColumn<>(columnName, fieldName, false);

columns.add(column);
}
}
}
}

public interface CustomColumn {

String[] getColumns();

Object getValueByColumnName(String columnName);
}

调整MapColumn的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

protected void getFieldData(String[] fieldNames,int start,Object child,int level,boolean isFoot) throws NoSuchFieldException, IllegalAccessException {

for (int i = start; i < fieldNames.length; i++) {
if (child == null) {
addData(null, isFoot);
countColumnValue(null);
getStructure().putNull(level,isFoot);
break;
}
if (child instanceof Map) {
child = ((Map) child).get(fieldNames[i]);
if (!isList(child)) {
if (i == fieldNames.length - 1) {
if (child == null) {
getStructure().putNull(level,isFoot);
}
T t = (T) child;
addData(t, true);
countColumnValue(t);
}
} else {
level++;
if (child.getClass().isArray()) {
isArrayColumn = true;
T[] data = (T[]) child;
setArrayType(ARRAY);
for (Object d : data) {
if (i == fieldNames.length - 1) {
addData((T) d, true);
} else {
getFieldData(fieldNames, i + 1, d, level, true);
}
}
getStructure().put(level - 1, data.length,isFoot);
} else {
List data = (List) child;
setArrayType(LIST);
isArrayColumn = true;
for (Object d : data) {
if (i == fieldNames.length - 1) {
T t = (T) d;
addData(t, true);
} else {
getFieldData(fieldNames, i + 1, d, level, true);
}

}
getStructure().put(level - 1, data.size(),isFoot);
}
break;
}
} else if (child instanceof MapTableData.CustomColumn) {
MapTableData.CustomColumn customColumn = (MapTableData.CustomColumn) child;
String fieldName = fieldNames[i];
child = customColumn.getValueByColumnName(fieldName);
if (!isList(child)) {
if (i == fieldNames.length - 1) {
if (child == null) {
getStructure().putNull(level,isFoot);
}
T t = (T) child;
addData(t, true);
countColumnValue(t);
}
} else {
level++;
if (child.getClass().isArray()) {
isArrayColumn = true;
T[] data = (T[]) child;
setArrayType(ARRAY);
for (Object d : data) {
if (i == fieldNames.length - 1) {
addData((T) d, true);
} else {
getFieldData(fieldNames, i + 1, d, level, true);
}
}
getStructure().put(level - 1, data.length,isFoot);
} else {
List data = (List) child;
setArrayType(LIST);
isArrayColumn = true;
for (Object d : data) {
if (i == fieldNames.length - 1) {
T t = (T) d;
addData(t, true);
} else {
getFieldData(fieldNames, i + 1, d, level, true);
}

}
getStructure().put(level - 1, data.size(),isFoot);
}
break;
}
}
}
}

实现model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

@Override
public String[] getColumns() {
return new String[]{"xx", "bb"};

}

@Override
public Object getValueByColumnName(String columnName) {
switch (columnName) {
case "xx":
return BZLotNo;
case "bb":
return jitai;

default:
return "未知:"+columnName;
}

}